<?php
class Integral{
	public $site_id = 0;
	//构造函数
	public function Integral($site_id = 0){
		if($site_id == 0){
		
			$site_id = defined('__ADMINSITEID__')?__ADMINSITEID__:__SITEID__;
		}
		
		$this->site_id = $site_id;
		
	
	}
	//创建设置项表单
	public function createform(){
		
		
	}
	
	//保存设置项
	public function saveform(){
		
	}
	
	//发起支付
	public function pay($orderid){
		
		$ordermes = M('Order')->where(array('id'=>$orderid, 'site_id' => $this->site_id))->find();
		$Setbody = $ordermes['ordernumber'].$ordermes['id'];
		$SetTotal_fee = $ordermes['total'];
		$member = M('Member') -> where(array('id' => $ordermes['member_id'])) -> find();
		//余额是否足够
		
		
		$member['money']=$member['online']+$member['offline'];
		
		if($member['money'] >= $SetTotal_fee){
			
		
			
			
			
			
			$ret = D('Order') -> upstate($ordermes['ordernumber']);
			if($ret){
				
			if($member['offline']>$SetTotal_fee){
				$data['user_id'] = 0;
				$data['member_id'] = $ordermes['member_id'];
				$data['remark'] = '支付订单'.$ordermes['ordernumber'];
				$data['total'] = abs($SetTotal_fee)*-1;
				$data['offline'] = abs($SetTotal_fee);
				$data['online'] = 0;
				$data['time'] = time();
				D('Integral_log') -> add($data);
				D('Member') -> where(array('id' => $ordermes['member_id'])) -> setDec('offline', $SetTotal_fee);
				$html = '<script>setTimeout(function(){$("#payPro").html("支付成功");
				$("#payend").show();},500);</script>';
				
				exit();
				
			}
				
				
				$data['user_id'] = 0;
				$data['member_id'] = $ordermes['member_id'];
				$data['remark'] = '支付订单'.$ordermes['ordernumber'];
				$data['total'] = abs($SetTotal_fee)*-1;
				$data['dateline'] = time();
				$data['offline'] = abs($member['offline'])*-1;
				$data['online'] = (abs($SetTotal_fee)-$member['offline'])*-1;
				
	
				D('Integral_log') -> add($data);
				
				
				D('Member') -> where(array('id' => $ordermes['member_id'])) -> setDec('offline', $SetTotal_fee);
				$html = '<script>setTimeout(function(){$("#payPro").html("支付成功");
				$("#payend").show();},500);</script>';
			}
		}else{
			$html = '<script>alert(\'支付失败！余额不足！\');</script>';
		}
		 
		return $html;
	}
	public function callback(){
	}
	
}

	

